package comu.hnevc.zh.testdemo.fragment;

import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import comu.hnevc.zh.testdemo.MainActivity;
import comu.hnevc.zh.testdemo.R;
import comu.hnevc.zh.testdemo.base.BaseUrl;
import comu.hnevc.zh.testdemo.bean.AllCarPeccancy;
import comu.hnevc.zh.testdemo.bean.CarInfo;
import comu.hnevc.zh.testdemo.bean.GetAllCarPeccancy;
import comu.hnevc.zh.testdemo.bean.GetPeccancyType;

public class CarPeccancyFragment extends Fragment {
    private MainActivity activity;
    private Button query_btn;
    private SharedPreferences sharedPreferences;
    private RequestQueue  requestQueue;
    private boolean logo=false;
    private String carnumber;
    private EditText carnumber_et;
    private int pcount;
    private int pmoney;
    private int pscode;
    private boolean log=true;
    public CarPeccancyFragment(){

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_car_peccancy,container,false);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @SuppressLint("WrongConstant")
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity= (MainActivity) getActivity();
        sharedPreferences=getContext().getSharedPreferences("data",Context.MODE_APPEND);
        query_btn=activity.findViewById(R.id.id_btn_query);
        carnumber_et=activity.findViewById(R.id.id_et_carnumber);
        query_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (logo){
                  carnumber="鲁"+carnumber_et.getText().toString();
                  initCarinfo();
                  if (pcount!=0){
                      getFragmentManager().beginTransaction()
                              .replace(R.id.id_content_view,new QueryPeccancyFragment())
                              .commit();
                  }else if(carnumber_et.getText().toString().equals("")){
                      Toast.makeText(activity,"车牌号不能为空",Toast.LENGTH_SHORT).show();

                  }
                }else {
                    Toast.makeText(activity,"暂未找到此车辆信息，请重新输入！",Toast.LENGTH_SHORT).show();
                }
            }
        });
        initData();

    }
    private void initCarinfo() {
        for (int i=0;i<activity.allCarPeccancieList.size();i++){
            if (activity.allCarPeccancieList.get(i).getCarnumber().equals(carnumber)){
                pcount++;
                pmoney+=activity.allCarPeccancieList.get(i).getPmoney();
                pscode+=activity.allCarPeccancieList.get(i).getPscode();
            }
        }
        //
        for (int i=0;i<activity.carInfoList.size();i++){
             if (activity.carInfoList.get(i).getCarnumber().equals(carnumber)){
                 log=false;
                 break;
             }
        }
        if (pcount!=0&&log){
            CarInfo carInfo=new CarInfo();
            carInfo.setCarnumber(carnumber);
            carInfo.setPmoneys(pmoney);
            carInfo.setPscodes(pscode);
            carInfo.setPcount(pcount);
            activity.carInfoList.add(carInfo);
        }
    }
    private void initData() {
        requestQueue=Volley.newRequestQueue(activity);
        final JSONObject object=new JSONObject();
        try {
            object.put("UserName",sharedPreferences.getString("UserName",""));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final JsonObjectRequest jor=new JsonObjectRequest(Request.Method.POST, BaseUrl.BASEURL + BaseUrl.GetAllCarPeccancy, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                final Gson gson=new Gson();
                GetAllCarPeccancy getAllCarPeccancy=gson.fromJson(jsonObject.toString(),GetAllCarPeccancy.class);
                final List<GetAllCarPeccancy.ROWSDETAILBean> peccancy=getAllCarPeccancy.getROWS_DETAIL();

                JsonObjectRequest jor1=new JsonObjectRequest(Request.Method.POST, BaseUrl.BASEURL + BaseUrl.GetPeccancyType, object, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Gson gson1=new Gson();
                        GetPeccancyType getPeccancyType=gson1.fromJson(jsonObject.toString(),GetPeccancyType.class);
                        List<GetPeccancyType.ROWSDETAILBean> type=getPeccancyType.getROWS_DETAIL();
                        activity.allCarPeccancieList.clear();
                        if (peccancy==null){
                            return;
                        }
                    for (int i=0;i<peccancy.size();i++){
                            for (int j=0;j<type.size();j++){
                            if (peccancy.get(i).getPcode().equals(type.get(j).getPcode())){
                                AllCarPeccancy allCarPeccancy=new AllCarPeccancy();
                                allCarPeccancy.setCarnumber(peccancy.get(i).getCarnumber());
                                allCarPeccancy.setPaddr(peccancy.get(i).getPaddr());
                                allCarPeccancy.setDatetime(peccancy.get(i).getDatetime());
                                allCarPeccancy.setPremarks(type.get(j).getPremarks());
                                allCarPeccancy.setPmoney(type.get(j).getPmoney());
                                allCarPeccancy.setPscode(type.get(j).getPscore());
                                activity.allCarPeccancieList.add(allCarPeccancy);
                                break;
                            }
                            }
                    }
                    logo=true;
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(activity,"违章代码网络访问错误",Toast.LENGTH_SHORT).show();
                    }
                });
             requestQueue.add(jor1);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(activity,"车辆信息网络访问错误",Toast.LENGTH_SHORT).show();

            }
        });
        requestQueue.add(jor);

    }


}
